#pragma once
#include <list>
#include "Vertex.h"
#include "Edge.h"
#include "GraphFather.h"


class MiniGraph : public GraphFather
{
private:
	list <Vertex*>			new_Vertexes;

	int						lastAddVertexes,
							lastAddEdges;

public:
	MiniGraph(bool directed) : GraphFather (directed)
	{
		lastAddVertexes	= 0;
		lastAddEdges	= 0;
		clear();
	}

	list <Vertex*> :: iterator newBegin()
	{
		return new_Vertexes.begin();
	}
	list <Vertex*> :: iterator newEnd()
	{
		return new_Vertexes.end();
	}

	void	clearAll_New_VertexesType() // test: COMPLETED
	{
		list <Vertex*> :: iterator It = new_Vertexes.begin();

		for(int q=0; q<vertexes.size(); q++, It++)
			(*It) -> Type = CLEAR;
	}
	void	addNewEdgeAndVertexes(Edge *newEdge)  // test: good (2 tests)
	{
		int				Connect1New		= inVertexesList (newEdge -> getFirstVertex() ),
						Connect2New		= inVertexesList (newEdge -> getSecondVertex());

		Vertex			*newVertex1		= NULL,
						*newVertex2		= NULL;

		if(Connect1New == -1)
			newVertex1 = new Vertex();
		else
		{
			list <Vertex*> :: iterator It = new_Vertexes.begin();
			for(int q=0; q<Connect1New; q++)
				It++;
			newVertex1 = *It;
		}

		
		if(Connect2New == -1)
			newVertex2 = new Vertex();
		else
		{
			list <Vertex*> :: iterator It = new_Vertexes.begin();
			for(int q=0; q<Connect2New; q++)
				It++;
			newVertex2 = *It;
		}

		Edge			*new_Edge	= new Edge	(newVertex1, newEdge -> getW());
		new_Edge	-> addVertex(newVertex2);

		newVertex1  -> addEdge					(new_Edge);
		if (!Directed)
			newVertex2  -> addEdge					(new_Edge);

		if(Connect1New == -1)
		{
			vertexes.push_back		(newEdge -> getFirstVertex() );
			new_Vertexes.push_back	(newVertex1);
		}
		if(Connect2New == -1)
		{
			vertexes.push_back		(newEdge -> getSecondVertex());
			new_Vertexes.push_back	(newVertex2);
		}


		edges.push_back		(new_Edge);

		lastAddVertexes =	(int)(Connect1New == -1) + (int)(Connect2New == -1);
		lastAddEdges	=	1;
	}
	void	CtrlZ() // test: good (2 tests)
	{
		while(lastAddVertexes > 0)
		{
			(*--new_Vertexes.end()) -> deletMe (Directed);
			new_Vertexes.erase	(--new_Vertexes.end());
			vertexes.erase		(--vertexes.end());
			lastAddVertexes--;
		}

		while(lastAddEdges > 0)
		{
			(*--edges.end()) -> getFirstVertex()  -> deletEdge (*--edges.end());
			(*--edges.end()) -> getSecondVertex() -> deletEdge (*--edges.end());
			edges.erase		(--edges.end());
			lastAddEdges--;
		}
	}

	~MiniGraph() // test: bag if create and delete 2 objects
	{
		list <Vertex*> :: iterator It3 = new_Vertexes.begin();
		for(int q=0; q<new_Vertexes.size(); q++, It3++)
			delete *It3;


		list <Edge*> :: iterator It2 = edges.begin();
		for(int q=0; q<edges.size(); q++, It2++)
			delete *It2;
	}
};


// -=BAIZOR=-